//
//  Pispatch_barrier_asyncViewController.m
//  JKThreadTest
//
//  Created by 王冲 on 2018/10/25.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "Dispatch_barrier_asyncViewController.h"

@interface Dispatch_barrier_asyncViewController ()

@property (strong, nonatomic) dispatch_queue_t queue;

@end

@implementation Dispatch_barrier_asyncViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.queue = dispatch_queue_create("rw_queue", DISPATCH_QUEUE_CONCURRENT);
    
    for (int i = 0; i<10; i++) {
        
        dispatch_async(self.queue, ^{
            
            [self read];
        });
        
        dispatch_async(self.queue, ^{
            
            [self write];
        });
        
    }
}

// 读文件
-(void)read{
    
    dispatch_async(self.queue, ^{
        
        sleep(1.0);
        NSLog(@"--读--");
    });
    
    
}

// 写文件
-(void)write{
   
    dispatch_barrier_async(self.queue, ^{
    
        sleep(1.0);
        
        NSLog(@"--写--");
        
    });
}

-(void)code{
    
    // 初始化自己定义的并发l队列
    dispatch_queue_t queue = dispatch_queue_create("rw_queue", DISPATCH_QUEUE_CONCURRENT);
    // 读
    dispatch_async(queue, ^{

    });
    // 写
    dispatch_barrier_async(queue, ^{
        
    });
    
}

@end
